package com.dhec.ptms.epr.db;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhec.ptms.epr.model.CheckBenchMarkDetail;
import com.dhec.ptms.epr.model.PrecisionDetail;
import com.ionglobal.db.DBI;
import com.ionglobal.lib.SysException;

public class PrecisionDetailDB {
	
	private static final Logger logger = LoggerFactory.getLogger(PrecisionDetailDB.class);
	
	public List<PrecisionDetail> list(DBI dbi, int roomId, int page, int pagesize){
		ArrayList<PrecisionDetail> list = new ArrayList<PrecisionDetail>(0);
		try {
			dbi.skipToPage(pagesize, page, "select * from EPR_PrecisionDetail where roomId="+roomId+" order by id asc");
			while(dbi.rsNext()){
				PrecisionDetail bean = new PrecisionDetail();
				this.handleData(dbi, bean);
				list.add(bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("PrecisionDetailDB.list(DBI, int, int, int) Error!", e);
		}
		
		return list;
		
	}
	
	public void handleData(DBI dbi, PrecisionDetail bean) throws SysException{
		bean.setId(dbi.rsGetInt("id"));
		bean.setItem(dbi.rsGetString("item"));
		bean.setRoomId(dbi.rsGetInt("roomId"));
		bean.setStandard(dbi.rsGetString("standard"));
		bean.setPractical(dbi.rsGetString("practical"));
		bean.setTestdate(dbi.rsGetDate("testdate"));
	}

	public long count(DBI dbi, int roomId){
		long c = 0;
		try {
			dbi.execQuery("select count(*) from EPR_PrecisionDetail where roomId="+roomId);
			if(dbi.rsNext()){
				c = dbi.rsGetLong(1);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("PrecisionDetailDB.count(DBI, int) Error!", e);
		}
		
		return c;
		
	}
	
	public boolean save(DBI dbi, PrecisionDetail bean){
		try {
			return dbi.execute("insert into EPR_PrecisionDetail (id, roomId, item, standard, practical, testdate)"
					+ " values("+bean.getId()+","+ bean.getRoomId()+",'"
					+dbi.fixSql(bean.getItem())+"',"
					+ "'"+dbi.fixSql(bean.getStandard())+"', '"+dbi.fixSql(bean.getPractical())+"',"+(bean.getTestdate()!=null?dbi.getInsertDate(bean.getTestdate()):"null")+")");
		} catch (SysException e) {
			logger.error("PrecisionDetailDB.save(DBI, PrecisionDetail) Error!", e);
		}
		
		return false;
		
	}
	
	public PrecisionDetail find(DBI dbi, int id){
		PrecisionDetail bean = null;
		try {
			dbi.execQuery("select * from EPR_PrecisionDetail where "
					+ "id="+id);
			if(dbi.rsNext()){
				bean = new PrecisionDetail();
				this.handleData(dbi, bean);
			}
			dbi.rsClose();
		} catch (SysException e) {
			logger.error("PrecisionDetailDB.find(DBI, int) Error!", e);
		}
		
		return bean;
		
	}
	
	public boolean update(DBI dbi, PrecisionDetail bean){
		try {
			return dbi.execute("update EPR_PrecisionDetail set item='"+dbi.fixSql(bean.getItem())+"',"
					+ "roomId="+bean.getRoomId()+","
					+ "standard='"+dbi.fixSql(bean.getStandard())+"'"
					+ " where id="+bean.getId());
		} catch (SysException e) {
			logger.error("PrecisionDetailDB.update(DBI, PrecisionDetail) Error!", e);
		}
		
		return false;
		
	}
	
	public boolean delete(DBI dbi, int roomId){
		try {
			return dbi.execute("delete from EPR_PrecisionDetail where roomId="+roomId);
		} catch (SysException e) {
			logger.error("PrecisionDetailDB.delete(DBI, int) Error!", e);
		}
		
		return false;
		
	}
}
